<template>
	<view class="content">
		<contont-nav-bar :isShowRed="isShowRed"></contont-nav-bar>
		<!-- <keyboard-listener @keydown="onKeydown"></keyboard-listener> -->
		<view class="index-content">
			<view class="content-list">
				<list-contont :clList="inventoryList" @onClickComfig="onClickComfig" @onDropLoad="onDropLoad"></list-contont>		
			</view>	
			<!-- 用户信息 -->
			<view class="content-user">
				<right-user-information :userInfo="userInfo" @onlogout="onlogout" @onInformation="onInformation"></right-user-information>
			</view>
		</view>

		<tab-bar :tabbarList="tabbarList" @onClick="onClick"></tab-bar>
		<!-- 盘点 -->
		<!-- <uni-popup ref="popup-inventory" type="center" :maskClick="false"> -->
		<uni-popup ref="popup-inventory" type="center">
			<view class="inventory">
				<view class="inventory-img"></view>
				<!-- <image class="inventory-img" src="../../static/1.gif" mode="widthFix"></image> -->
				<view class="inventory-text">正在盘点</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import { workWithData } from '../../utils/tool.js'
	import { loadLogout, getState, getPlasticGoods, getInventory, inventoryOperation, abnormalStock,getysToken, getysImage } from '../../utils/api.js'
	import keyboardListener from '../../components/keyboard-listener/keyboard-listener.vue'
	import uniPopup from '@/components/uni-popup/uni-popup.vue'
	import RightUserInformation from '../../components/common/right-user-information.vue'
	import ListContont from '../../components/common/list-contont.vue'
	import ContontNavBar from '../../components/common/contont-nav-bar.vue'
	import TabBar from '../../components/common/tab-bar.vue'
	export default {
		components: {
			TabBar,
			ContontNavBar,
			ListContont,
			RightUserInformation,
			uniPopup,
			keyboardListener
		},
		data() {
			return {
				accessToken: '',
				timeImageId:null,
				terminalId:null,
				inventoryList:[],
				text: '',
				userId: null,
				isShowRed: true, //控制盘点中导航栏图标颜色
				userInfo:{}, //用户信息
				clList: [
					{
						operTime: "2016-12-20 18:39:49",
						itemTypeName: "电子器材1",
						operatorName: "王晓斌",
						seatCode: "A12D3",
						type: false, //true:出柜  false:入柜
						isAuthorization: true   //是否授权
					}
				],
				tabbarList: [
					{
						src: "../../static/images/tabbar/11.png",
						path: "../cabinet/cabinet"
					},
					{
						src: "../../static/images/tabbar/22.png",
						path: "../authorization/authorization"
					},
					{
						src: "../../static/images/tabbar/33.png",
						path: "../historical-data/historical-data"
					},
					{
						src: "../../static/images/tabbar/44.png",
						path: "../inventory/inventory"
					},
				]
			}
		},
		onLoad(options) {
			console.log(options)
			this.getYSToken()
			let tokenData = uni.getStorageSync('lastToken');
			this.terminalId = tokenData.terminalId
			console.log(tokenData)
			this.userInfo.userName = tokenData.userName
			this.userInfo.deptName = tokenData.deptName
			this.userInfo.avatar = tokenData.avatar
			console.log(tokenData.userId,tokenData.userName,tokenData.deptName,tokenData.avatar);
			
		}, 
		onReady() {
			let tokenData = uni.getStorageSync('lastToken');
			this.terminalId = tokenData.terminalId
			getInventory({
				terminalId: this.terminalId
			}).then(res=>{
				this.inventoryList=res.data
			})
			// this.$store.dispatch("inventoryData",{terminalId:tokenData.terminalId,antenna:tokenData.rfid_id})
			// this.loadCheck()
			// module.writeCoil({ //写入状态(更改)
			// 	'saveId':0,
			// 	'start':0,
			// 	'value':false  //判断柜门开着还关
			// },(res1)=>{
			// 	console.log("柜门打开",res1)
			// 	this.loadCheck()
			// })
		},
		methods: {
			getYSToken(){
				getysToken({
					appKey:'8b7fdfdb47ac4ad9b083b970a4f7c230',
					appSecret:'0040958c1ded2326da582120aa2e2ac4'
				}).then(res=>{
					console.log(res);
					this.accessToken = res.data.data.accessToken
					this.$store.commit('setYSToken',res.data.data)
					
					this.loadGetImage()
					this.timeImageId = setInterval(()=>{
						this.loadGetImage()
					},1000*60*25)

				})
				// this.ystoKen = data.data.accessToken
			},
			
			loadGetImage() {
				console.log(this.accessToken);
				getysImage({
					accessToken: this.accessToken,
					deviceSerial: 'E95470902',
					channelNo: 1
				}).then(res=>{
					console.log(res);
					let obj = {...res.data.data}
					obj.time = this.forMart()
					obj.isSelected = false
					if(obj.picUrl) {
						setTimeout(()=>{
							this.$store.commit('addImage',obj)
							console.log(obj);
						},1000*60*3)
					}
				})
			},
			onClick(index) {
				uni.redirectTo({
					url: this.tabbarList[index].path
				})
			},
			loadCList(){
				if(this.inventoryList.length<9){
					for(let i=this.inventoryList.length;i<9;i++){
						this.inventoryList[i]={datetime:0}
					}
				}
			},
			onlogout() {
				//退出登录把本地token清空，返回欢迎登录页面
				let thid = this
				uni.showModal({
					title: '退出提示',
					content: '确认退出吗?',
					confirmText: '确认',
					success: function (res){ 
						if (res.confirm) {
							let tokenData = uni.getStorageSync('token'+thid.terminalId);
							// clearInterval(thid.timeImageId)
							uni.showLoading({
								title: '正在盘点.....'
							});
							thid.$store.dispatch("inventoryData",{terminalId:tokenData.terminalId,antenna:tokenData.rfid_id,ismian:minor.ismian,index:minor.index,callback:function(){
								console.log(tokenData)
								uni.hideLoading();
								loadLogout({terminalId:tokenData.terminalId}).then(data=>{
									clearInterval(tokenData.terminalId)
									uni.removeStorageSync(tokenData.terminalId)
									console.log(data)
									uni.redirectTo({
										url: '../welcome-login/welcome-login'
									})
									// if(data.code!==0){
									// 	uni.showToast({
									// 		title:'退出失败'
									// 	})
									// }								
								})
							}})
						} else {} 
					}
				})	
			},
			onInformation() {
				uni.redirectTo({
					url: '../information-center/information-center'
				})
			}, 
			onClickComfig(index) {
				console.log('点击了',index);
			},
			onDropLoad() {   //下拉加载
				
			},
			forMart(){
				let dates = new Date()
				let year = dates.getFullYear()
				let month = dates.getMonth() + 1
				let day = dates.getDate()
				let hour = dates.getHours() < 10 ? "0" + dates.getHours() : dates.getHours()
				let minute = dates.getMinutes() < 10 ? "0" + dates.getMinutes() : dates.getMinutes()
				let seconds = dates.getSeconds() < 10 ? "0" + dates.getSeconds() : dates.getSeconds()
				month >= 1 && month <= 9 ? (month = "0" + month) : ""
				day >= 0 && day <= 9 ? (day = "0" + day) : ""
				return year+'-'+month+'-'+day+' '+hour+':'+minute+':'+seconds
			}
		}
	}
</script>

<style scoped lang="less">
	.content{
		background: #F4F4F4;
		height: 100vh;
		position: fixed;
		// height: 1384rpx;
	}
	.index-content{
		display: flex;
		justify-content: space-between;
	}
	.content-list{	
		width: 2290rpx;
		height: 1050rpx; 
		margin: 34rpx 48rpx 56rpx 52rpx;
		box-shadow: 0px 2px 10px 0px #F4F4F4;
		// overflow: auto;
		box-sizing: border-box;	
	}
	.content-user{
		width: 480rpx;
		height: 1090rpx;
		background: #FFFFFF;
		border-radius: 0 0 8rpx 32rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		box-shadow: 0px 1px 1px 0px #D9E4F8;
	}
	.inventory{
		position: fixed;
		top: 50%;
		left: 50%;
		transform: translateX(-50%) translateY(-50%);
		width: 390rpx;
		height: 390rpx;
		background: transparent;
		display: flex;
		align-items: center;
		flex-direction: column;
		justify-content: center;
		.inventory-img{
			width: 300rpx;
			height: 248rpx;
			background: url(../../static/1.gif) no-repeat;
			background-size: cover;
			margin-bottom: 40rpx;
		}
		.inventory-text{
			font-size: 40rpx;
			color: #2180EA;
		}
	}
</style>
